(function() {
    var $IconUpload = function(pictureId) {
        var reservationsId = document.getElementById("reservationsId").value;
        var customerId = document.getElementById("customerId").value;
        var payServiceId = document.getElementById("payServiceId").value;
        if (payServiceId == null || payServiceId == "") {
            payServiceId = "aa"
        }
        var iconType = document.getElementById("iconType").value;
        var remarks = document.getElementById("remarks").value;
        if (remarks == null || remarks == "") {
            remarks = "aa"
        }
        var finnallyTime = document.getElementById("finnallyTime").value;
        var cri = payServiceId + "," + customerId + "," + reservationsId + "," + iconType + "," + remarks + "," + finnallyTime;
        this.pictureId = pictureId;
        this.uploadBtnId = pictureId + "BtnId";
        this.uploadPreId = pictureId + "PreId";
        this.uploadUrl = Feng.ctxPath + '/doctorLooked/uploadIcon/' + cri;
        this.picWidth = 800;
        this.picHeight = 800;
        this.uploadBarId = null
    };
    $IconUpload.prototype = {
        initIcon: function() {
            var uploader = this.create();
            this.bindEvent(uploader);
            return uploader
        },
        create: function() {
            var webUploader = WebUploader.create({
                auto: true,
                pick: {
                    id: '#' + this.uploadBtnId,
                    multiple: true,
                },
                accept: {
                    title: 'Images',
                    extensions: 'gif,jpg,jpeg,bmp,png,dcm',
                    mimeTypes: 'image/gif,image/jpg,image/jpeg,image/bmp,image/png,image/dcm'
                },
                swf: Feng.ctxPath + '${ctxPath}/static/css/plugins/webuploader/Uploader.swf',
                disableGlobalDnd: true,
                duplicate: true,
                server: this.uploadUrl,
                fileSingleSizeLimit: this.fileSizeLimit
            });
            return webUploader
        },
        bindEvent: function(bindedObj) {
            var id = $("#customerId").val();
            var me = this;
            bindedObj.on('fileQueued', function(file) {
                var $li = $('<div><img width="100px" height="100px"></div>');
                var $img = $li.find('img');
                $("#" + me.uploadPreId).html($li);
                bindedObj.makeThumb(file, function(error, src) {
                    if (error) {
                        $img.replaceWith('<span>不能预览</span>');
                        return
                    }
                    $img.attr('src', src)
                }, me.picWidth, me.picHeight)
            });
            bindedObj.on('uploadProgress', function(file, percentage) {
                $("#" + me.uploadBarId).css("width", percentage * 100 + "%")
            });
            bindedObj.on('uploadSuccess', function(file, response) {
                Feng.success("上传成功");
                if (parent.$("#isDorj").val() == 'tc') {
                    if (parent.$("#detail").val() == 'isArrive') {
                        parent.location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/6/0';
                    } else {
                        parent.location.href = Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/7/0';
                    }
                }else if (parent.$("#isDorj").val() == 'ys'){
                    window.parent.location.reload();
                }
                $("#" + me.pictureId).val(response)
            });
            bindedObj.on('uploadError', function(file) {
                Feng.error("上传失败")
            });
            bindedObj.on('error', function(type) {
                if ("F_EXCEED_SIZE" == type) {
                    Feng.error("文件大小不能大于100KB")
                } else if ("Q_TYPE_DENIED" == type) {
                    Feng.error("文件类型不满足")
                } else if ("Q_EXCEED_NUM_LIMIT" == type) {
                    Feng.error("上传数量超过限制")
                } else if ("F_DUPLICATE" == type) {
                    Feng.error("图片选择重复")
                } else {
                    Feng.error("上传过程中出错")
                }
            });
            bindedObj.on('uploadComplete', function(file) {})
        },
        setIconUploadBarId: function(id) {
            this.uploadBarId = id
        }
    };
    window.$IconUpload = $IconUpload
}());